home *** CD-ROM | disk | FTP | other *** search
- /***** BEGIN LICENSE BLOCK *****
-
- FlashGot - a Firefox extension for external download managers integration
- Copyright (C) 2004-2009 Giorgio Maone - g.maone@informaction.com
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
- ***** END LICENSE BLOCK *****/
-
- var gFlashGot = {
-
- onload: function(ev) {
- ev.currentTarget.removeEventListener(ev.type, arguments.callee, false);
- try {
- gFlashGot.init();
- } catch(e) {
- dump("FlashGot init error: " + e.message);
- gFlashGot.log("Unrecoverable init error: " + e.message + " --- " + e.stack);
- }
- },
-
-
- hoverElement: null,
-
- _isContentEvent: function(ev) {
- var d = ev.originalTarget.ownerDocument;
- return d && d.defaultView && d.defaultView.top == window.content;
- },
-
- init: function() {
- if(!gFlashGotService) throw new Error("FlashGotService not registered!");
-
- gFlashGotService.dom._winType = document.documentElement.getAttribute("windowtype");
-
- // install listeners
- gFlashGot.isMouseDown = false;
- var target = window.gBrowser || window;
-
- if (target.tabContainer) {
- target.tabContainer.addEventListener("TabSelect", this.updateMediaStatus, false);
- }
-
-
-
- target.addEventListener("load", this.updateMediaStatus, true);
-
- target.addEventListener("mousedown", function(ev) {
- if (!gFlashGot._isContentEvent(ev)) return;
- gFlashGot.isMouseDown = true;
- gFlashGot.hoverElement = ev.originalTarget;
- }, false);
-
- target.addEventListener("mouseover", function(ev) {
- if(!gFlashGot.isMouseDown) gFlashGot.hoverElement = ev.originalTarget;
- }, false);
-
- target.addEventListener("submit", function(ev) {
- if (!gFlashGot._isContentEvent(ev)) return;
- var f = ev.originalTarget;
- if(/#FlashGot_form$/.test(f.action) ||
- f.ownerDocument.defaultView.location.hash == "#FlashGot_Form")
- gFlashGotService.interceptor.forceAutoStart = true;
- }, false);
-
- target.addEventListener("mouseup", function(ev) {
- gFlashGot.isMouseDown = false;
-
- if (!gFlashGot._isContentEvent(ev)) return;
-
- gFlashGotService.interceptor.bypassAutoStart = false;
- gFlashGotService.interceptor.forceAutoStart = false;
- if(ev.altKey && !(ev.ctrlKey || ev.metaKey)) {
- function prevent() {
- ev.preventDefault();
- ev.stopPropagation();
- gFlashGot.lastClickCaptureTime = new Date().getTime();
- }
- var invert = gFlashGotService.getPref("invertAltShiftClick", false);
- if((ev.shiftKey && !invert) || (invert && !ev.shiftKey)) {
- gFlashGotService.interceptor.bypassAutoStart =
- gFlashGotService.getPref("bypassCombo", true);
- return;
- } else {
- if(gFlashGotService.getPref("altClick")) {
- try {
- if(gFlashGot.download()) {
- prevent();
- return;
- }
- } catch(ex) {}
- gFlashGotService.interceptor.forceAutoStart = true;
- } else {
- return;
- }
- }
-
- ev2 = ev.view.document.createEvent("MouseEvents");
- ev2.initMouseEvent("click",ev.canBubble, ev.cancelable,
- ev.view, ev.detail, ev.screenX, ev.screenY,
- ev.clientX, ev.clientY,
- //ev.ctrlKey, ev.altKey, ev.shiftKey, ev.metaKey,
- false,false,false,false,
- ev.button, ev.relatedTarget);
- prevent();
- gFlashGot.hoverElement.dispatchEvent(ev2);
- }
- }, true);
-
- target.addEventListener("click", function(ev) {
- if(ev.altKey && ev.originalTarget.ownerDocument != document &&
- typeof(gFlashGot.lastClickCaptureTime) == "number" &&
- new Date().getTime() - gFlashGot.lastClickCaptureTime < 100
- ) {
- ev.preventDefault();
- ev.stopPropagation();
- }
- }, true);
-
-
- this.contextMenu.addEventListener("popupshowing", function(ev) {
- if(this == ev.explicitOriginalTarget) {
- gFlashGot.prepareContextMenu(ev);
- }
- },false);
- this.contextMenu.addEventListener("popuphidden", function(ev) {
- if(this == ev.explicitOriginalTarget) {
- gFlashGot.disposeContextMenu(ev);
- }
- }, false);
-
- this.toggleMainMenuIcon();
-
- gFlashGotService.checkVersion();
-
- if(typeof(flashgot_doDirtyJobs) == "function") {
- window.setTimeout(flashgot_doDirtyJobs,0);
- }
- }
- ,
- log: function(msg) {
- gFlashGotService.log(msg);
- }
- ,
- get contextMenu() {
- var cm =
- document.getElementById("contentAreaContextMenu") ||
- document.getElementById("mailContext") || // TB3
- document.getElementById("messagePaneContext"); // TB2
- if (cm) {
- delete this.contextMenu;
- this.contextMenu = cm;
- }
- return cm;
- }
- ,
- switchOption: function(opt) {
- gFlashGotService.setPref(opt, !gFlashGotService.getPref(opt));
- }
- ,
- openOptionsDialog: function() {
- window.open("chrome://flashgot/content/flashgotOptions.xul", "flashgotOptions",
- "chrome,dialog,centerscreen,alwaysRaised");
- }
- ,
- openAboutDialog: function() {
- window.open("chrome://flashgot/content/about.xul", "flashgotAbout",
- "chrome,dialog,centerscreen");
- }
- ,
- browse: function(url) {
- var browser = window.getBrowser();
- browser.selectedTab = browser.addTab(url);
- }
- ,
- browseHomePage: function() {
- this.browse("http://flashgot.net");
- }
- ,
- get hideIcons() {
- return gFlashGotService.getPref("hide-icons", false);
- }
- ,
- toggleIcon: function(m, hide) {
- if(!m) return;
- const iconicClass = m.tagName + "-iconic";
- const rx=new RegExp("\\b"+iconicClass+"\\b");
- if(hide) {
- m.className=m.className.replace(rx, "").replace(/\bflashgot-icon-(\w+)\b/,'flashgot-noicon-$1');
- } else {
- const cl=m.className;
- if(!rx.test(cl)) {
- m.className=cl.replace(/\bflashgot-noicon-(\w+)\b/,'flashgot-icon-$1')+" "+iconicClass;
- }
- }
- }
- ,
- toggleMainMenuIcon: function() {
- this.toggleIcon(document.getElementById("flashgot-menu"),this.hideIcons);
- }
- ,
- prepareToolsMenu: function(ev) {
-
- function toggleMenu(id,disabled) {
- id = "flashgot-main-menuitem-" + id;
- var m = document.getElementById(id);
- if(!m) return;
- m.setAttribute("disabled", disabled);
- gFlashGot.toggleIcon(m, hideIcons);
- }
-
-
-
- if(gFlashGotService && !(document.getElementById("flashgot-menu").hidden = gFlashGotService.getPref("hide-menu"))) {
- const dis = !gFlashGotService.DMS.found;
- const hideIcons = this.hideIcons;
- this.updateMediaUI();
-
- toggleMenu("tabs", dis || !this.isTabbed);
- toggleMenu("all", dis);
- toggleMenu("sel", dis || this.isSelInvalid);
- toggleMenu("buildGallery", false);
- toggleMenu("media", dis || !this.media);
- toggleMenu("opts", false);
- this.prepareOptsMenu(ev.target);
-
- }
- }
- ,
-
- prepareContextMenu: function(ev) {
-
- this.toggleMainMenuIcon();
- var menuCount = 0;
-
- function menuSwitch(name, disabled) {
- var menuItem = document.getElementById("flashgot-menuitem-" + name);
-
- if(menuItem &&
- ! ( menuItem.hidden =
- ( hidden || gFlashGotService.getPref("hide-" + name) ) )
- ) {
- menuItem.setAttribute("disabled", disabled ? "true" : "false");
- if(! (menuItem.hidden=disabled && hideDisabled) ) {
- menuCount++;
- }
- gFlashGot.toggleIcon(menuItem, hideIcons);
- }
- }
- var menuItem = null;
- const defaultDM = gFlashGotService.defaultDM;
- const dms = gFlashGotService.DMS;
- var dm = dms.found ? dms[defaultDM] : null;
-
- gFlashGotService.restoreNativeUIs(document);
- dm.hideNativeUI(document);
-
- const invalidLink = !this.popupLink;
-
- const invalidSel = this.isSelInvalid;
-
- const noLink = this.linksCount == 0;
- const hideDisabled = gFlashGotService.getPref("hideDisabledCmds");
-
-
- var hidden = !(dms.found && dm);
- const hideIcons = this.hideIcons;
-
- var hideLink = invalidLink || hidden || dm.disabledLink;
- var hideSel = invalidSel || hidden || dm.disabledSel;
- this.prepareCommandsMenu(document.getElementById("flashgot-menuitem-it"), hideLink && hideSel);
-
- this.updateMediaUI();
-
- menuSwitch("it", hideLink);
- menuSwitch("sel", hideSel);
- menuSwitch("all", noLink || hidden || dm.disabledAll);
- menuSwitch("tabs", (! (typeof(gBrowser)=="object"
- && gBrowser.browsers && gBrowser.browsers.length > 1) )
- || hidden || dm.disabledAll);
- menuSwitch("media", hidden || !this.media);
-
- hidden = false;
- menuSwitch("buildGallery", false);
-
- const optsMenu = document.getElementById("flashgot-menu-options");
- this.toggleIcon(optsMenu,hideIcons);
- if(!(optsMenu.hidden = gFlashGotService.getPref("hide-options"))) {
- menuCount++;
- }
-
- const submenu = document.getElementById("flashgot-submenu");
- this.toggleIcon(submenu, hideIcons);
- const subanchor = document.getElementById("flashgot-submenu-anchor");
- const subpop = subanchor.parentNode;
- const sep1 = document.getElementById("flashgot-context-separator");
- const sep2 = document.getElementById("flashgot-context-separator2");
- const menu = sep1.parentNode;
- var next = null;
-
- const nested = gFlashGotService.getPref("nested-menu") && (menuCount > 1);
- submenu.hidden = !nested;
- if(nested) {
- menuCount=0;
- if(!subanchor.nextSibling) {
- menuItem = document.getElementById("flashgot-menuitem-tabs");
- if (menuItem)
- menuItem.setAttribute("accesskey",
- document.getElementById("flashgot-main-menuitem-tabs").getAttribute("accesskey"));
- for(menuItem = sep1.nextSibling;
- menuItem && (menuItem != sep2);
- menuItem = next) {
- next = menuItem.nextSibling;
- subpop.appendChild(menuItem);
- }
- }
- } else {
- menuItem = document.getElementById("flashgot-menuitem-tabs");
- if (menuItem) menuItem.removeAttribute("accesskey");
- for(menuItem = subanchor.nextSibling; menuItem; menuItem = next) {
- next = menuItem.nextSibling;
- menu.insertBefore(menuItem, sep2);
- }
- }
- sep1.hidden = menuCount == 0;
- }
- ,
- disposeContextMenu: function(ev) {
- gFlashGotService.restoreNativeUIs(document);
- },
- prepareCommandsMenu: function(anchorNode, hideOnly) {
- var node, parentNode = anchorNode.parentNode;
-
- var cmi = (parentNode.getElementsByClassName && parentNode.getElementsByClassName("flashgot-command-menuitem")
- || parentNode.getElementsByTagName("menuitem"));
-
- var j, len;
-
- for(j = cmi.length; j-- > 0;)
- if (/^flashgot-command-mi-/.test(cmi[j].id))
- parentNode.removeChild(cmi[j]);
-
- if(hideOnly) return;
-
- var mi, dm, id;
- const dms = gFlashGotService.DMS;
- for(j = 0, len = dms.length; j < len; j++) {
- dm = dms[j];
- if(dm.supported && dm.shownInContextMenu) {
- id = "flashgot-command-mi-" + dm.codeName;
- if (!document.getElementById(id)) {
- mi = document.createElement("menuitem");
- mi.setAttribute("class", "menuitem-iconic flashgot-command-menuitem");
- mi.setAttribute("label", dm.name);
- mi.setAttribute("id", id);
- mi.setAttribute("oncommand", "gFlashGot.downloadSel(this.label) || gFlashGot.downloadPopupLink(this.label)");
- parentNode.insertBefore(mi, anchorNode);
- }
- dm.hideNativeUI(document);
- }
- }
- },
-
-
- prepareOptsMenu: function(parentNode) {
-
- const opts = parentNode.getElementsByTagName("menuitem");
-
- this.toggleIcon(document.getElementById("flashgot-tbb-menuitem-opts"), this.hideIcons);
-
- var menuItem, id, match, lastMenu=null, isTBB=false;
- var j = opts.length;
- while(j-- > 0) {
- menuItem = opts[j];
- if((id=menuItem.id)) {
- if((match = id.match(/opt-(.*)/))) {
- menuItem.setAttribute("checked",
- gFlashGotService.getPref(match[1]) ? "true" : "false");
- } else if((match = id.match(/^flashgot-(\w+)-menuitem-nodms$/))) {
- lastMenu = menuItem;
- isTBB = match[1]=="tbb";
- }
- }
- }
-
- if(!lastMenu) return;
-
- const defaultDM = gFlashGotService.defaultDM;
- const dms = gFlashGotService.DMS;
- var menuItemId;
- if(dms.found) {
- var idPrefix="flashgot-menuopt-dm-";
- var eventPostfix;
- if(isTBB) {
- idPrefix += "tbb-";
- eventPostfix = "gFlashGot.downloadSel() || gFlashGot.downloadAll()";
- } else {
- eventPostfix = "gFlashGot.downloadSel() || gFlashGot.downloadPopupLink()";
- }
- lastMenu.setAttribute("hidden", "true");
- parentNode = lastMenu.parentNode;
- var dm;
- const miclass = "flashgot-dms-entry";
- // add menu items
- for(j = dms.length; j-- >0;) {
- dm = dms[j];
- if(dm.supported) {
- menuItemId = idPrefix + dm.codeName;
- menuItem = document.getElementById(menuItemId);
- if(!menuItem) {
- menuItem = document.createElement("menuitem");
- menuItem.setAttribute("class", miclass);
- menuItem.setAttribute("id", menuItemId);
- menuItem.setAttribute("type", "radio");
- menuItem.setAttribute("autocheck", "true");
- menuItem.setAttribute("oncommand",
- "gFlashGotService.defaultDM = this.label; window.setTimeout(function() { " +
- eventPostfix + " }, 0)");
- menuItem.setAttribute("label", dm.name);
- parentNode.insertBefore(menuItem, lastMenu);
- }
- menuItem.setAttribute("checked", (defaultDM == dm.name) ? "true" : "false");
- lastMenu = menuItem;
- }
- }
- // remove menu items
- var nodes=parentNode.getElementsByAttribute("class", miclass);
- for(j=nodes.length; j-->0;) {
- dm=dms[nodes[j].getAttribute("label")];
- if(!(dm && dm.supported)) {
- parentNode.removeChild(nodes[j],true);
- }
- }
- } else {
- lastMenu.removeAttribute("hidden");
- }
- }
- ,
-
-
-
- _mediaUI: null,
- updateMediaUI: function() {
- if (!this._mediaUI) {
- this._mediaUI = ["flashgot-menuitem-media", "flashgot-main-menuitem-media","flashgot-media-status"]
- .map(function(id) { return document.getElementById(id); });
- }
- var mm = this.media;
- var count = (mm && mm.length) ? " (" + mm.length + ")" : "";
- var tip = (count && typeof(/ /) == "object") // Fx >= 3, multiline tooltips supported
- ? mm.map(function(l) { return l.tip }).join("\n")
- : '';
-
- var l;
- for each(var ui in this._mediaUI) {
- if (ui) {
- l = ui.getAttribute("label");
- if (l) ui.label = l.replace(/\s*\(\d+\)$/, '') + count;
-
- if (tip) ui.setAttribute("tooltiptext", tip);
- else ui.removeAttribute("tooltiptext");
-
- if (count) ui.hidden = ui.disabled = false;
- }
- }
- this.updateMediaStatus(true);
- },
-
- updateMediaStatus: function(anim) {
- if (this != gFlashGot) gFlashGot.updateMediaStatus();
- else {
- var ms = this.mediaStatusAnim.widget;
- if (ms) {
- if (gFlashGotService.getPref("media.statusIcon", true)) {
- var m = this.media;
- ms.hidden = ms.disabled = !(m && m.length);
- } else {
- ms.hidden = true;
- }
- this.mediaStatusAnim.run(!anim);
- }
- }
- },
-
- mediaStatusAnim: {
- interval: 0,
- lastShowing: false,
- cycles: 0,
- get widget() {
- delete this.widget;
- return this.widget = document.getElementById("flashgot-media-status");
- },
- run: function(once) {
- var w = this.widget;
- if (!w) return;
- var showing = !w.hidden;
- if (showing) {
- var opacity = 0;
- if (!this.lastShowing) {
- this.cycles = 3;
- this.lastShowing = true;
- opacity = 1;
- }
- if (this.cycles <= 0) return;
- var opacity = opacity || parseFloat(w.style.opacity) || 1;
- if (opacity <= .1) {
- opacity = 1;
- this.cycles --;
- } else {
- opacity -= .05;
- }
- w.style.opacity = opacity;
- if (!once)
- window.setTimeout(function() { gFlashGot.mediaStatusAnim.run(); }, 50);
- } else this.lastShowing = false;
- }
- },
-
- prepareMediaMenu: function(menu) {
- while (menu.firstChild) menu.removeChild(menu.firstChild);
- var m = this.media;
- if (!(m && m.length)) return false;
-
-
- var mi;
- for (var j = 0; j < m.length; j++) {
- menu.appendChild(this._createMediaMenuItem(m[j]));
- }
- return true;
- },
- _createMediaMenuItem: function(l) {
- mi = document.createElement("menuitem");
- mi.setAttribute("label", l.label);
- mi.setAttribute("tooltiptext", l.tip);
- mi.addEventListener("command", function() { gFlashGot.downloadMedia([l]); }, false);
- return mi;
- },
-
-
- get srcWindow() {
- return document.commandDispatcher.focusedWindow;
- }
- ,
- get srcDocument() {
- return this.srcWindow.document;
- }
- ,
-
- get isTabbed() {
- var b = window.gBrowser;
- return b && b.browsers && b.browsers.length > 1;
- }
- ,
- get isSelInvalid() {
- return this.srcWindow.getSelection().isCollapsed && !this.grabSelectedTextFields(null, true);
- }
- ,
- get popupLink() {
- return this.findLinkAsc(document.popupNode);
- }
- ,
- get linksCount() {
- const doc = this.srcDocument;
- if(!doc) return 0;
-
- var count = doc.links && doc.links.length || 0;
-
- if(gFlashGotService.getPref("includeImages"))
- count += (doc.images && doc.images.length) || 0;
- count += (doc.embeds && doc.embeds.length) || 0;
-
- return count;
- }
- ,
- getLinks: function(filter, includeImages, doc) {
- if(typeof(doc) != "object") {
- doc = this.srcDocument;
- }
- if(doc == null) return [];
- const allLinks = [];
- const lm = gFlashGotService.lookupMethod;
-
-
- function wrapAndFilter(newL, l) {
- const href = l.src;
- if (!href) return null;
-
- if(href.lastIndexOf("://", 9) < 0) {
- try {
- newL.href =
- (uriResolver ||
- (uriResolver =
- Components.classes['@mozilla.org/network/io-service;1']
- .getService(Components.interfaces.nsIIOService)
- .newURI(doc.URL, null, null))).resolve(href);
- } catch(ex) {
- return false;
- }
- } else {
- newL.href = href
- }
-
- const ga = lm(l, "getAttribute");
- var des = ga("alt") || ga("title") || href.substring(href.lastIndexOf("/") + 1);
- var w, h;
- (w = ga("width")) && (h = ga("height")) && (des += " (" + w + "," + h + ")");
-
- newL.description = des;
-
- return filter(newL, l);
- }
-
- function filterLinks(elems, filter, tagName) {
- const wrap = tagName == "A";
- try {
- if(elems) {
- var l, newL;
- const e_item = lm(elems,"item");
- for(var j = 0, len = lm(elems,"length")(); j < len; j++) {
- l = e_item(j);
- newL = wrap ? gFlashGot._wrapAnchor(l, lm) : { tagName: tagName };
- if(newL && filter(newL, l)) {
- allLinks[allLinks.length] = newL;
- }
- }
- }
- } catch(ex) {}
- }
-
-
-
- filterLinks(doc.links, filter, "A");
-
- var uriResolver = null;
-
- if(includeImages) {
- filterLinks(doc.images, wrapAndFilter,"IMG");
- }
-
- filterLinks(doc.embeds,wrapAndFilter,"EMBED");
-
- return allLinks;
- }
- ,
- get referrer() {
- if(this._referrer) return this._referrer;
- var docURL = this.srcDocument.URL;
- var gb = docURL && docURL.substring(0,5) == "file:" && this.getBuildGalleryData();
- return gb ? gb.referrer : docURL;
- }
- ,
- set referrer(r) {
- return this._referrer = r;
- },
-
- getBuildGalleryData: function(doc) {
- doc = doc || window.content.document;
- const props = ['preview', 'content', 'referrer'];
- var gb = {};
- try {
- for each(var p in props) {
- gb[p] = doc.getElementById(p).firstChild.nodeValue;
- }
- } catch(e) {
- gb = null;
- }
- return gb;
- },
-
- grabSelectedTextFields: function(selection, justCheck) {
- const lm = gFlashGotService.lookupMethod;
- const doc = this.srcDocument;
- var ff, f, j;
- const vv = [];
- var selStart, selEnd;
- for each(var t in ["textarea", "input"]) {
- var ff = doc.getElementsByTagName(t);
- for(j = 0; (f = ff[j]); j++) {
- try {
- if(selection && selection.containsNode(f, true)) {
- if(justCheck) return true;
- vv.push(lm(f, "value")());
- }
- else {
- selStart = lm(f, "selectionStart")(), selEnd = lm(f, "selectionEnd")();
- if(selStart < selEnd) {
- if(justCheck) return true;
- vv.push(lm(f, "value")()).substring(selStart, selEnd);
- }
- }
- } catch(e) {}
- }
- }
- return justCheck ? false : vv;
- }
- ,
- getSelectionLinks: function(includeImages) {
- const selection = this.srcWindow.getSelection();
-
- // link nodes detection
- var links = this.getLinks(function(link, trueNode) {
- return link && gFlashGot.checkLink(link) &&
- selection.containsNode(trueNode ? trueNode : link, true);
- }, includeImages);
-
- var selString = selection.toString();
-
- // add textboxes
-
- selString += this.grabSelectedTextFields(selection).join("\n");
-
- var m;
-
- // password detection
- var pwd = gFlashGotService.getPref("selection.guessPassword", true) &&
- (m = selString.match(/\b(?:password|passw|pass|pwd|pw)\W+(.*)/i)) && m[1];
-
- // text links detection
- m = selString.match(
- /\b(?:(?:h.{2}p|ftp|mms|rtsp|rtmpe?):\/\/|[a-z]+[a-z\d]+\.[a-z\d\-\.]+\/)[^\s]*/gi);
- delete selString;
- var j, k;
- if(m) {
- var descMap = null;
- var href, desc;
- var d, diff;
-
- linksLoop:
- for(j = 0, len = m.length; j < len; j++) {
- desc = m[j];
- href = desc.replace(/^h.{2}p/i, "http").replace(/^([a-z]+[a-z\d]+\.[a-z\d\.]+\/)/i, "http://$1");
- // TODO: riddles like http://rap*dshare.com
- if(href) {
- if(!descMap) { // we use it to avoid textual "quasi-duplicates", as http://somepart...oftheurl
- descMap = {};
- for(k = links.length; k-- > 0;) {
- descMap[links[k].description] = true;
- }
- }
- if (descMap[href] || descMap[desc]) continue;
- if (!/^https?:/.test(desc)) {
- for (d in descMap) {
- diff = d.length - desc.length;
- if (diff >= 0 && d.indexOf(desc) == diff) continue linksLoop;
- }
- }
- links[links.length] = { href: href, description: m[j] };
- }
- }
- }
-
- if(pwd) {
- var des;
- pwd = " pw: " + pwd;
- for(j = links.length; j-- > 0;) {
- des = links[j].description || "";
- links[j].description = des.substring(0, 4) == "http" ? pwd : des.concat(pwd);
- }
- }
- return links;
- }
- ,
- checkLink: function(link) {
- return link.href && /^[a-z]+:\/\/.*|javascript:.*http/i.test(link.href) && !/^(mailto|news|file|data):/i.test(link.href);
- }
- ,
- _wrapAnchor: function(node, lm) {
- var isAnchor = (node instanceof HTMLAnchorElement);
- if(isAnchor || node instanceof HTMLAreaElement) {
- var d;
- var href = lm(node, "href")();
- if(href) return {
- href: href,
- tagName: "A",
- getElementsByTagName: lm(node, "getElementsByTagName"),
- description: isAnchor
- ? (d = lm(node,"title")()) ||
- (d = lm(node, "innerHTML")()) && d.replace(/<[^>]*>/g, "")
- : (d = (lm(node, "alt")() || lm(node, "title")()))
- };
- }
- return null;
- }
- ,
- findLinkAsc: function(node) {
- const lm = gFlashGotService.lookupMethod;
- var anchor;
- while(node) {
- anchor = this._wrapAnchor(node, lm);
- if(anchor) return this.checkLink(anchor) ? anchor : null;
- node = node.parentNode;
- }
- return null;
- }
- ,
- delayCmd: function(cmd) {
- const pg = this.createProgress();
- pg.update(5);
- window.setTimeout(function() {
- try {
- pg.value = 100;
- gFlashGot["download"+cmd]();
- gFlashGot.showProgress();
- } catch(ex) {
- dump(ex);
- }
- },0);
- }
- ,
-
- downloadPopupLink: function(dmName) {
- const link = this.popupLink;
- return link && this.download([link], gFlashGotService.OP_ONE, dmName);
- }
- ,
- downloadPopupNodeText: function(dmName) {
- if (!(document.popupNode && document.popupNode.textContent)) return false;
- return this.download([{
- href: document.popupNode.textContent,
- description: "Thunderbird Link"
- }], gFlashGotService.OP_ONE, dmName);
- }
- ,
- downloadLink: function(dmName) {
- const sel=this.srcWindow.getSelection();
- var link = !sel.isCollapsed && this._wrapAnchor(sel.anchorNode, gFlashGotService.lookupMethod)
- || this.findLinkAsc(this.hoverElement) || this.findLinkAsc(document.commandDispatcher.focusedElement);
- return link && this.download([link], gFlashGotService.OP_ONE, dmName);
- }
- ,
- downloadSel: function(dmName) {
- if(this.isSelInvalid) return false;
- const startTime = new Date().getTime();
- const links = this.getSelectionLinks(gFlashGotService.getPref("includeImages"));
- if(!links.length) return false;
- links.startTime = startTime;
- return this.download(links, gFlashGotService.OP_SEL, dmName);
- }
- ,
- WrappedCollection: function(coll) {
- this.item = gFlashGotService.lookupMethod(coll, "item");
- this.length = gFlashGotService.lookupMethod(coll, "length")();
- }
- ,
- collectAllLinks: function(doc, tagName) {
- var links = [];
- const includeImages = gFlashGotService.getPref("includeImages");
- if(tagName) {
- var frames = new this.WrappedCollection(doc.getElementsByTagName(tagName));
- const lm = gFlashGotService.lookupMethod;
- var contentDocument;
- for(var j=frames.length; j-->0;) {
- try {
- if((contentDocument = lm(frames.item(j), "contentDocument")())) {
- links = links.concat(
- this.collectAllLinks(contentDocument));
- }
- } catch(ex2) {
- }
- }
- } else {
- links = links.concat(this.getLinks(this.checkLink, includeImages, doc)
- ).concat(this.collectAllLinks(doc, "frame")
- ).concat(this.collectAllLinks(doc, "iframe"));
- this.addMediaLinks(links);
- }
- return links;
- }
- ,
- get media() {
- return window.content._flashgotMedia || null;
- },
-
- addMediaLinks: function(links, mm) {
- mm = mm || this.media;
- if (!(mm && mm[0])) return false;
- if (!(links.length || links.referrer)) links.referrer = mm[0].referrer;
- Array.prototype.push.apply(links, mm);
- return true;
- }
- ,
- downloadAll: function(dmName) {
- const startTime = new Date().getTime();
- const links = this.collectAllLinks(content.document);
- links.startTime = startTime;
- return this.download(links, gFlashGotService.OP_ALL, dmName);
- }
- ,
- downloadTabs: function(dmName) {
- if(!this.isTabbed) return this.downloadAll(dmName);
- const bb = getBrowser().browsers;
- var doc;
- var links=[];
- for (var j = 0, len = bb.length; j<len; j++)
- if ((doc = bb[j].contentDocument))
- links = links.concat(this.collectAllLinks(doc));
-
- return links.length &&
- this.download(links, gFlashGotService.OP_ALL, dmName);
- }
- ,
- downloadMedia: function(mediaLinks, dmName) {
- var links = [];
- return this.addMediaLinks(links, mediaLinks) &&
- this.download(links, gFlashGotService.OP_SEL, dmName);
- }
- ,
- download: function(links, opType, dmName) {
- if (!links) {
- // best guess selection/link
- return this.downloadSel() || this.downloadLink();
- }
-
- try {
- links.referrer = links.referrer || this.referrer;
- links.browserWindow = window;
- links.document = this.srcDocument;
- } catch(ex) {}
-
- links.progress = this.createProgress();
- const ret = gFlashGotService.download(links, opType, dmName);
- if(!ret) links.progress.update(100);
- return ret;
- }
- ,
- progressList: [],
- createProgress: function(v) {
- return new this.Progress(v);
- },
- Progress: function(v) {
- this.value = v || 0;
- this.showing = false;
- this.update = function(v) {
- if(!this.showing) {
- this.showing = true;
- gFlashGot.progressList.push(this);
- }
- if(typeof(v) == "number") this.value = v;
- gFlashGot.showProgress();
- }
- },
-
- showProgressValue: function(v) {
- document.getElementById("flashgot-progresspanel").setAttribute("collapsed",
- v < 100 ? "false" : "true");
- document.getElementById("flashgot-progressmeter").value = v;
- gFlashGotService.yield();
- },
-
- showProgress: function() {
- const pgl = this.progressList;
- var len = pgl.length;
- var value;
- if(len > 0) {
- value = 0;
- for(var j = len; j-- > 0;) {
- var v = pgl[j].value;
- if(v < 100) {
- value += v;
- } else {
- pgl.splice(j, 1);
- len--;
- }
- }
- value = len > 0 ? Math.round(value / len) : 100;
- } else {
- value = 100;
- }
- this.showProgressValue(value);
- }
- ,
- buildGallery: function() {
- var previewURL = null, contentURL = null;
- var gb = this.getBuildGalleryData();
- if(gb) {
- dump("FGBG: reusing gallery data\n");
- previewURL = gb.preview;
- contentURL = gb.content;
- } else {
- var links=this.getSelectionLinks(true);
- if(!(links && links.length)) {
- dump("FGBG: no selection links, using ");
- if(this.popupLink) {
- dump("popup link\n");
- links = [this.popupLink];
- } else {
- dump("all links\n");
- links=this.getLinks(this.checkLink, true);
- }
- }
- var len;
- if(links && (len=links.length)) {
- const previewRX = /\d+.*\.(jpg|jpeg|png|gif|bmp)(\?|$)/i;
- const contentRX = /\d+.*\.[a-z0-9]{2,4}(\?|$)/i;
- const lm = gFlashGotService.lookupMethod;
- var l, tag, url, imgs, i, iLen, i_item, imgSrc;
- for(var j=0; j<len && ! (contentURL && previewURL); j++) {
- l=links[j];
- tag=l.tagName && l.tagName.toUpperCase();
- url=l.href;
- if(tag!="IMG" && contentRX.test(url)) {
- contentURL=url;
- if(tag=="A" && l.getElementsByTagName ) {
- imgs=l.getElementsByTagName("img");
- i_item=lm(imgs, "item");
- for(i = 0, iLen = lm(imgs,"length")(); i < iLen; i++) {
- imgSrc = lm(i_item(i),"src")();
- if(previewRX.test(imgSrc)) {
- previewURL = imgSrc;
- break;
- }
- }
- }
- }
- }
- if( (!previewURL) && (tag=="img" || previewRX.test(url)) ) {
- previewURL=url;
- }
- }
- if(!previewURL) previewURL="";
- if(previewURL && !contentURL) contentURL=previewURL;
- }
- window.openDialog("chrome://flashgot/content/flashgotGalleryBuilder.xul","_blank",
- "chrome,dialog,centerscreen,resizable",
- {
- previewURL: previewURL,
- contentURL: contentURL,
- referrerURL: this.referrer,
- originalWindow: window,
- tmpDir: gFlashGotService.tmpDir,
- prefs: gFlashGotService.prefs
- }
- );
- }
-
- }
-
- window.addEventListener("load", gFlashGot.onload, false);
-
-
-
-